/* JavaScript Metadata Dictionary Initialization */

/* Define metadata dictionary values below. This can be done in the following ways:
	- Directly in the presentation layer or on the page 
	- Automatically, using autoassignments from page-specific content (DOM, URL query string, cookies, etc)
*/

var _initJSMDconfig={
	/*  Basic config - simple skeleton metadata hierarchy. All page, session, user, and other pertinent metadata will be stored in this container
		and leveraged by various analytics solutions. */
	basic_config:  "{page:{},product:{},user:{},promo:{}};",
	version: "20100314",
	
	/* Vendor-neutral data collection routines go below. Make certain all the data collection routines start with a lower-case g */
	mdCollection: {
		/* gData:  // This function is built-in and automatically returns data from metadata objects */
		gQuery: function(param,url){var b,m,l,k,j,i,h,g,f,e,d,c;b=(url!=null?url:window.location.href);b=b.split('?');m=b.length;if(m>1){for(l=0;l<m;l++){i=b[l].split('&');j=i.length;for(k=0;k<j;k++){g=i[k].split('=');if(g[0]==param)h=g[1]}f=b[l].split('%26');e=f.length;for(d=0;d<e;d++){c=f[d].split('%3d');if(c[0]==param)h=c[1]}}}h=h?h:'';return h},
		gCookie: 	function(param,flag) { var r=this.getcookie(param);if(flag=='c'||flag==1) this.setcookie(param,"-","-1"); return r},
		gDOM:		function(domstring) {try { return eval(domstring); } catch(err) {}; return ""; }, 
		gMeta: 		function(tag) { 
		var mtc=this.metatags;if(mtc==null){var mt=document.getElementsByTagName("meta"),i,mtc={};for(i=mt.length-1;i>=0;i--){if(mt[i].name.length>0)mtc[mt[i].name]=(mt[i].content?mt[i].content:"")}
		this.metatags=mtc}
		return(mtc[tag]==null?"":mtc[tag]); }
	},
	
	/* Vendor-neutral data transformation routines go below.  They will be added to the root context of the closure. */
	mdTransform: {
		tCase: function(arg) { 	return arg.toLowerCase();		// Case transformation function
		},
		tTrim: function(arg) { return arg.replace(/\s+/g," ").replace(/^\s/,"").replace(/\s$/,""); // Process whitespaces - trim & pump out extra whitespaces 
		},
		tSub: function(arg,delimiter,i) { var r=""; try{r=arg.split(delimiter)[i];}catch(err) {}; return r;
		},
		tAll: "tCase|tTrim" // The "tAll" transform is a special transform and will be applied to all functions automatically
							// Note, you can only have a single ALL function - multiple "all" functions are not supported.
	},
	
	/* Other built-in functions: 
		function loadValues(configObject);
			- Allows the user to pass in a configuration JSON object with absolute metadata values, 
	*/	
	common: {
		"getcookie":function(k){var c=' '+document.cookie,s=c.indexOf(' '+k +'='),e=s<0?s:c.indexOf(';',s),v=s<0?'':c.substring(s+2 +k.length,e<0?c.length:e);return v},
		"setcookie":function(k,v,e,d){var exp=new Date((new Date().getTime())+e*86400000); document.cookie=k+"="+escape(v)+ (e!=null?"; expires="+exp.toGMTString():"") +"; path=/;"+(d!=null?" domain="+d:"");},
		transform_delimiter: "|", 
		argument_delimiter:","
	}
};	


function _initJSMD(initialValues) {
	var t=window._initJSMDconfig,i,i2,_jsmd,alltrfm=[],t2,t3,a1;
	_jsmd=eval("(function(){ return "+t.basic_config+"})();");
	_jsmd.about={version:t.version};
	_jsmd.common=t.common;
	for(i in t.mdCollection) {_jsmd.common[i]=t.mdCollection[i];}
	t2=t.mdTransform;
	for(i in t2)  {
		if(typeof(t2[i])=='string')  {a1=t2[i].split("|"); for(i2=a1.length-1;i2>=0;i2--) alltrfm.push(t2[a1[i2]]); _jsmd.common[i]=function(arg) {var targ=arg,i; for(i=alltrfm.length-1;i>=0;i--) targ=alltrfm[i](targ); return targ;}; }
		else {
			t3=t2[i];_jsmd.common[i]=t3;
		}
	}
	_jsmd.loadValues=function(config) {
		var k,v,v1,x,ta=this.common.tAll;
		for(k in config) {
			v=config[k];
			if(typeof(v)=='string') { v="'"+this._pvt_transParam(v)+"'";}
			if(typeof(v)=='object') { v1=this._pvt_transParam(v[0]); v="this.common."+v[1].split("arg").join("'"+v1+"'"); if(v.indexOf("'")!=-1) v.replace(/[\']/g,"\\'");} 
			try { if(ta) {eval("this."+k+"=this.common.tAll("+v+");");} else {eval("this."+k+"="+v+";");} } catch(err) { };
		}
	}
	_jsmd._pvt_transParam=function(str2) {
		var t=this.common.transform_delimiter,t2,val,t3,subre=/\(\s*([^\)]+)\s*\,\s*([^\)]+)\s*\)/;
		try {
			if(str2.indexOf(t)!=-1) {
				t2=str2.split(t);
				if(t2[0]=="gData") return eval("this."+t2[1]);
				/* if((t3=subre.test(t2[1]))!=null) {
					
				} */
				val=this.common[t2[0]](t2[1]);
			} else val=str2;
		} catch(err) {};
		return val;
	}
	if(initialValues) _jsmd.loadValues(initialValues);
	return _jsmd;	
}
